<template>
  <div>
    <h1>文章列表</h1>
    <el-table :data="articles" style="width: 100%">
      <el-table-column prop="_id" label="ID" width="260"> </el-table-column>
      <el-table-column prop="title" label="文章名称"> </el-table-column>
      <el-table-column prop="categories" label="所属分类">
        <template slot-scope="scope">
          {{ scope.row.categories.map((item) => item.name).join() }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">
            编辑
          </el-button>
          <el-button @click="handleDelete(scope.row)" type="text" size="small">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

  <script>
export default {
  data() {
    return {
      articles: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.$http.get("rest/articles").then((res) => {
        if (res.status === 200) {
          this.articles = res.data;
        }
      });
    },
    handleClick(row) {
      this.$router.push("/articles/edit/" + row._id);
    },
    handleDelete(row) {
      this.$confirm(`确定要删除 ${row.name} 吗`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http.delete("rest/articles/" + row.id).then((res) => {
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getList();
          }
        });
      });
    },
  },
};
</script>